package com.ruoyi.project.mtool.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.mtool.domain.Product;
import com.ruoyi.project.tool.gen.domain.GenTable;
import com.ruoyi.project.tool.gen.domain.GenTableColumn;
import com.ruoyi.project.tool.gen.util.GenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mtool.mapper.CustomerProductMapper;
import com.ruoyi.project.mtool.domain.CustomerProduct;
import com.ruoyi.project.mtool.service.ICustomerProductService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 客户产品关联Service业务层处理
 *
 * @author ruoyi
 * @date 2020-04-08
 */
@Slf4j
@Service
public class CustomerProductServiceImpl implements ICustomerProductService {
    @Autowired
    private CustomerProductMapper customerProductMapper;

    /**
     * 查询客户产品关联
     *
     * @param id 客户产品关联ID
     * @return 客户产品关联
     */
    @Override
    public CustomerProduct selectCustomerProductById(Long id) {
        return customerProductMapper.selectCustomerProductById(id);
    }

    /**
     * 查询客户产品关联列表
     *
     * @param customerProduct 客户产品关联
     * @return 客户产品关联
     */
    @Override
    public List<CustomerProduct> selectCustomerProductList(CustomerProduct customerProduct) {
        return customerProductMapper.selectCustomerProductList(customerProduct);
    }

    /**
     * 新增客户产品关联
     *
     * @param customerProduct 客户产品关联
     * @return 结果
     */
    @Override
    public int insertCustomerProduct(CustomerProduct customerProduct) {
        customerProduct.setCreateTime(DateUtils.getNowDate());
        return customerProductMapper.insertCustomerProduct(customerProduct);
    }

    /**
     * 修改客户产品关联
     *
     * @param customerProduct 客户产品关联
     * @return 结果
     */
    @Override
    public int updateCustomerProduct(CustomerProduct customerProduct) {
        customerProduct.setUpdateTime(DateUtils.getNowDate());
        return customerProductMapper.updateCustomerProduct(customerProduct);
    }

    /**
     * 批量删除客户产品关联
     *
     * @param customerIds 需要删除的客户产品关联ID
     * @return 结果
     */
    @Override
    public int deleteCustomerProductByIds(Long[] customerIds) {
        return customerProductMapper.deleteCustomerProductByIds(customerIds);
    }

    /**
     * 删除客户产品关联信息
     *
     * @param customerId 客户产品关联ID
     * @return 结果
     */
    @Override
    public int deleteCustomerProductById(Long customerId) {
        return customerProductMapper.deleteCustomerProductById(customerId);
    }


    /**
     * 导入客户产品关联信息
     *
     * @param productList 导入表列表
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importCustomerProduct(List<Product> productList, Long customerId) {
        String operName = SecurityUtils.getUsername();
        CustomerProduct customerProduct = null;
        for (Product product : productList) {
            try {
                customerProduct = new CustomerProduct();
                customerProduct.setCustomerId(customerId);
                customerProduct.setProductId(product.getProductId());
                customerProduct.setCreateBy(operName);
                customerProductMapper.insertCustomerProduct(customerProduct);
            } catch (Exception e) {
                log.error("产品 " + product.getProductName() + " 导入失败：", e);
            }
        }
    }
}
